{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n",
      "error\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding: utf-8\n",
    "\n",
    "import pandas as pd\n",
    "from pandas import *\n",
    "import datetime\n",
    "import json\n",
    "from pymongo import MongoClient\n",
    "from collections import defaultdict\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "client = MongoClient('localhost', 27017)\n",
    "db = client.futures\n",
    "indexMarket = db.indexMarket\n",
    "peaks = db.peak\n",
    "unit=db.unit\n",
    "\n",
    "start='20190601'\n",
    "# var='JD'\n",
    "\n",
    "indexMarket = DataFrame(list(indexMarket.find({'date': {'$gte': start}})))\n",
    "\n",
    "unit = DataFrame(list(unit.find()))\n",
    "dd=unit['variety']\n",
    "for i in set(dd):\n",
    "    try:\n",
    "        df=indexMarket[indexMarket['variety']==i]\n",
    "#         date=df[['date'][-1]]\n",
    "        df= df[['date', 'variety','set_open','set_close', 'set_high', 'set_low']]\n",
    "        df.set_index('date',inplace=True)\n",
    "        maxs=df[['set_high','set_low']].stack().max()\n",
    "        mins=df[['set_high','set_low']].stack().min()\n",
    "        gains=round(((maxs/mins-1)*100),2)\n",
    "        lesses=round(((1-mins/maxs)*100),2)\n",
    "        peak=(lambda x:gains if mins < data['set_close'][-1] else lesses)(1)\n",
    "        df2=df.copy()\n",
    "        df2['maxs']=maxs\n",
    "        df2['mins']=mins\n",
    "        df2['peak']=peak\n",
    "        print(i)\n",
    "        df2 = df2.reset_index()\n",
    "        df2=df2[['date','variety','maxs','mins','set_close','peak']][-1:]\n",
    "        \n",
    "#         peaks.insert(json.loads(df2.T.to_json()).values())\n",
    "#         print(json.loads(df2.T.to_json()).values())\n",
    "#         print(1)\n",
    "    except:\n",
    "        print('error')\n",
    "df=DataFrame(list(peaks.find()))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
